Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | import fs from 'fs/promises' import path from 'path' import { NextResponse } from 'next/server' import { deleteColumnClassifierSample } from '@/lib/vision/trainingDataDeletion' import { withAuth } from '@/lib/auth/withAuth' /** * Directory where collected training data is stored */ const TRAINING_DATA_DIR = path.join(process.cwd(), 'data', 'vision-training', 'collected') /** * GET /api/vision-training/images/[digit]/[filename] * * Serves a training image file. */ export const GET = withAuth( async (_request, { params }) => { try { const { digit, filename } = (await params) as { digit: string; filename: string } // Validate digit if (!/^[0-9]$/.test(digit)) { return NextResponse.json({ error: 'Invalid digit' }, { status: 400 }) } // Validate filename (prevent path traversal) if (filename.includes('..') || filename.includes('/') || !filename.endsWith('.png')) { return NextResponse.json({ error: 'Invalid filename' }, { status: 400 }) } const filePath = path.join(TRAINING_DATA_DIR, digit, filename) try { const data = await fs.readFile(filePath) return new NextResponse(new Uint8Array(data), { headers: { 'Content-Type': 'image/png', 'Cache-Control': 'public, max-age=31536000, immutable', }, }) } catch { return NextResponse.json({ error: 'Image not found' }, { status: 404 }) } } catch (error) { console.error('[vision-training] Error serving image:', error) return NextResponse.json({ error: 'Failed to serve image' }, { status: 500 }) } }, { role: 'admin' } ) /** * PATCH /api/vision-training/images/[digit]/[filename] * * Reclassifies a training image by moving it to a different digit folder. * Body: { newDigit: number } */ export const PATCH = withAuth( async (request, { params }) => { try { const { digit, filename } = (await params) as { digit: string; filename: string } const body = await request.json() const { newDigit } = body // Validate current digit if (!/^[0-9]$/.test(digit)) { return NextResponse.json({ error: 'Invalid current digit' }, { status: 400 }) } // Validate new digit if ( typeof newDigit !== 'number' || newDigit < 0 || newDigit > 9 || !Number.isInteger(newDigit) ) { return NextResponse.json({ error: 'Invalid new digit' }, { status: 400 }) } // Validate filename (prevent path traversal) if (filename.includes('..') || filename.includes('/') || !filename.endsWith('.png')) { return NextResponse.json({ error: 'Invalid filename' }, { status: 400 }) } // No-op if same digit if (String(newDigit) === digit) { return NextResponse.json({ success: true, reclassified: false, message: 'Same digit', }) } const srcPath = path.join(TRAINING_DATA_DIR, digit, filename) const destDir = path.join(TRAINING_DATA_DIR, String(newDigit)) const destPath = path.join(destDir, filename) // Ensure destination directory exists await fs.mkdir(destDir, { recursive: true }) try { // Move the file await fs.rename(srcPath, destPath) return NextResponse.json({ success: true, reclassified: true, oldDigit: parseInt(digit, 10), newDigit, filename, newImageUrl: `/api/vision-training/images/${newDigit}/${filename}`, }) } catch (error) { if ((error as NodeJS.ErrnoException).code === 'ENOENT') { return NextResponse.json({ error: 'Image not found' }, { status: 404 }) } throw error } } catch (error) { console.error('[vision-training] Error reclassifying image:', error) return NextResponse.json({ error: 'Failed to reclassify image' }, { status: 500 }) } }, { role: 'admin' } ) /** * DELETE /api/vision-training/images/[digit]/[filename] * * Deletes a training image file and records to tombstone. */ export const DELETE = withAuth( async (_request, { params }) => { try { const { digit, filename } = (await params) as { digit: string; filename: string } // Validate digit format (detailed validation in shared function) if (!/^[0-9]$/.test(digit)) { return NextResponse.json({ error: 'Invalid digit' }, { status: 400 }) } const result = await deleteColumnClassifierSample(parseInt(digit, 10), filename) if (!result.success) { return NextResponse.json({ error: result.error }, { status: 400 }) } if (!result.deleted) { return NextResponse.json({ error: 'Image not found' }, { status: 404 }) } return NextResponse.json({ success: true, deleted: filename, tombstoneRecorded: result.tombstoneRecorded, warning: result.tombstoneRecorded ? undefined : result.error, }) } catch (error) { console.error('[vision-training] Error deleting image:', error) return NextResponse.json({ error: 'Failed to delete image' }, { status: 500 }) } }, { role: 'admin' } ) |